package DroidEye.DAO;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import DroidEye.HelloApplication;
import DroidEye.DAO.DAOInterface.StudentDAOInterface;
import DroidEye.Dto.Apply;
import DroidEye.Dto.Project;
import DroidEye.Dto.Student;
import DroidEye.JavaBean.ApplyBean;
import DroidEye.JavaBean.ProjectBean;
import DroidEye.JavaBean.StudentBean;
import DroidEye.Mapper.ApplyMapper;
import DroidEye.Mapper.ProjectMapper;
import DroidEye.Mapper.StudentMapper;

@Service
public class StudentDAO implements StudentDAOInterface {
    @Autowired
    private StudentMapper studentMapper;
    
    @Autowired
    private ProjectMapper projectMapper;
    
    @Autowired
    private ApplyMapper applyMapper;

    @Override
    public StudentBean queryStudentAccount(String studentID) {
        Student student = studentMapper.selectByPrimaryKey(studentID);
        if(student == null) {
            return null;
        }
        return custStudent(student);
    }

    @Override
    public boolean changeStudentPassword(String studentID, String newStudentPassword) {
        Student param = new Student();
        param.setStudentId(studentID);
        param.setStudentPassword(newStudentPassword);
        int num = studentMapper.updatePasswordById(param);
        if(num > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<ProjectBean> queryAllProject() {
        List<ProjectBean> projectBeanList = new ArrayList<>();
        List<Project> projectList = projectMapper.queryAllProject();
        if(CollectionUtils.isEmpty(projectList)) {
            return projectBeanList;
        }
        return custProjectList(projectList);
    }

    @Override
    public List<ApplyBean> queryAllSelectedProject() {
        List<ApplyBean> applyList = new ArrayList<ApplyBean>();
        List<Apply> applys = applyMapper.queryAllApply();
        if(CollectionUtils.isEmpty(applys)) {
            return applyList;
        }
        return custApplyList(applys);
    }

    @Override
    public StudentBean querySingleStudent(String studentID) {
        Student student = studentMapper.selectByPrimaryKey(studentID);
        return custAllInfoStudent(student);
    }

    @Override
    public boolean updateStudent(StudentBean studentBean, String studentID) {
        Student param = new Student();
        param.setStudentId(studentID);
        param.setStudentSex(studentBean.getStudentSex());
        param.setStudentClass(studentBean.getStudentClass());
        param.setStudentPhonenumber(studentBean.getStudentPhoneNumber());
        int num = studentMapper.updateByPrimaryKeySelective(param);
        if(num > 0) {
            return true;
        }
        return false;
    }

    @Override
    public ApplyBean querySelectedProjectApplyInfo(String studentID) {
        Apply apply = applyMapper.queryApplyByStudentId(studentID);
        ApplyBean applyBean = custApply(apply);
        applyBean.setProjectTotalScore(apply.getProjectTotalScore());
        return applyBean;
    }

    @Override
    public ProjectBean querySelectedProject(String projectID) {
        Project project = projectMapper.queryByProjectId(projectID);
        if(project == null) {
            return new ProjectBean();
        }
        return custProject(project);
    }

    @Override
    public boolean addSelectedProject(ApplyBean applyBean) {
        Apply param = new Apply();
        param.setStudentId(applyBean.getStudentID());
        param.setProjectId(applyBean.getProjectID());
        param.setTeacherId(applyBean.getTeacherID());
        param.setStudentName(applyBean.getStudentName());
        param.setProjectName(applyBean.getProjectName());
        param.setTeacherName(applyBean.getTeacherName());
        param.setProjectContent(applyBean.getProjectContent());
        int num = applyMapper.insertSelective(param);
        if(num > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean setSelectedProjectPass(String studentID) {
        Apply param = new Apply();
        param.setPassStatus("通过");
        param.setStudentId(studentID);
        int num = applyMapper.updateStatusByStudentId(param);
        if(num > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean setSelectedProjectAdjuNumber(String studentID, String adjuNumber) {
        Apply param = new Apply();
        param.setStudentId(studentID);
        param.setAdjuNumber(adjuNumber);
        int num = applyMapper.updateAdjuNumberByStudentId(param);
        if(num > 0) {
            return true;
        }
        return false;
    }

    @Override
    public String getSelectedProjectAdjuNumber(String studentID) {
        Apply apply = applyMapper.queryApplyByStudentId(studentID);
        if(apply == null) {
            return "";
        }
        return apply.getAdjuNumber();
    }
    
    // 转换Student为StudentBean
    private StudentBean custStudent(Student student) {
        StudentBean studentBean = new StudentBean();
        studentBean.setStudentID(student.getStudentId());
        studentBean.setStudentPassword(student.getStudentPassword());
        studentBean.setStudentName(student.getStudentName());
        return studentBean;
    }
    
    // 转换List<Project>为List<ProjectBean>
    private List<ProjectBean> custProjectList(List<Project> projectList){
        List<ProjectBean> projectBeanList = new ArrayList<>();
        for(Project project : projectList) {
            ProjectBean projectBean = custProject(project);
            projectBeanList.add(projectBean);
        }
        return projectBeanList;
    }

    // 转换Project为ProjectBean
    private ProjectBean custProject(Project project){
        ProjectBean projectBean = new ProjectBean();
        projectBean.setProjectID(project.getProjectId());
        projectBean.setProjectName(project.getProjectName());
        projectBean.setProjectInfo(project.getProjectInfo());
        projectBean.setTeacherID(project.getTeacherId());
        return projectBean;
    }

    // 转换List<Apply>为List<ApplyBean>
    private List<ApplyBean> custApplyList(List<Apply> applyList) {
        List<ApplyBean> applyBeanList = new ArrayList<>();
        for(Apply apply : applyList) {
            ApplyBean applyBean = custApply(apply);
            applyBeanList.add(applyBean);
        }
        return applyBeanList;
    }

    // 转换Apply为ApplyBean
    private ApplyBean custApply(Apply apply) {
        ApplyBean applyBean = new ApplyBean();
        applyBean.setStudentID(apply.getStudentId());
        applyBean.setProjectID(apply.getProjectId());
        applyBean.setTeacherID(apply.getTeacherId());
        applyBean.setStudentName(apply.getStudentName());
        applyBean.setProjectName(apply.getProjectName());
        applyBean.setTeacherName(apply.getTeacherName());
        applyBean.setProjectContent(apply.getProjectContent());
        applyBean.setProjectProgress(apply.getProjectProgress());
        applyBean.setProjectTeach(apply.getProjectTeach());
        applyBean.setPassStatus(apply.getPassStatus());
        applyBean.setProjectTotalScore(apply.getProjectTotalScore());
        applyBean.setAdjuNumber(apply.getAdjuNumber());
        return applyBean;
    }

    // 转换Student为StudentBean
    private StudentBean custAllInfoStudent(Student student) {
        StudentBean studentBean = custStudent(student);
        studentBean.setStudentSex(student.getStudentSex());
        studentBean.setStudentClass(student.getStudentClass());
        studentBean.setStudentPhoneNumber(student.getStudentPhonenumber());
        return studentBean;
    }
    
    public static StudentDAO getStudentDAOInstance(){
        return HelloApplication.getBean("studentDAO", StudentDAO.class);
    }
}
